#!/bin/bash
# Author: liaojunhua 
# QQ: 563293401
# rsyslogx client create

# Source function library.
. /etc/init.d/functions

Ser_Name=$1

rsyslog_server="192.168.80.50"
rsyslog_dir="/etc/rsyslog.d"

micro_dir="/opt/service"
micro_proname=$(ls $micro_dir 2>/dev/null)

tomcat_dir="/opt/tomcat"
tomcat_proname=$(ls $tomcat_dir 2>/dev/null)

imof_dir="/opt/ofComponent"
imof_proname=$(ls $imof_dir 2>/dev/null)

#use info
service_usage () {
    echo "Micro Usage: $0 micro|tomcat|imof"
    exit 2
}

create_micro () {
    for m in $micro_proname
    do
cat > ${rsyslog_dir}/${m}.conf <<EOF
input(type="imfile"
File="/opt/logs/${m}.log"
Tag="${m}-tlog"
Facility="local4"
Severity="notice"
PersistStateInterval="1"
reopenOnTruncate="on")
EOF
	done

}

create_tomcat () {
    for t in $tomcat_proname
    do
cat > ${rsyslog_dir}/${t}.conf <<EOF
input(type="imfile"
File="${tomcat_dir}/${t}/logs/catalina.out"
Tag="${t}-tlog"
Facility="local4"
Severity="notice"
PersistStateInterval="1"
reopenOnTruncate="on")
EOF
    done

}

create_imof () {
    for im in $imof_proname
    do
cat > ${rsyslog_dir}/${im}.conf <<EOF
input(type="imfile"
File="${imof_dir}/${im}/logs/out.log"
Tag="${im}-tlog"
Facility="local4"
Severity="notice"
PersistStateInterval="1"
reopenOnTruncate="on")
EOF
    done

}

case "$1" in
    micro )
        [ $# -ne 1 ] && { service_usage; exit; }
        create_micro
        ;;

    tomcat )
        [ $# -ne 1 ] && { service_usage; exit; }
        create_tomcat
        ;;
        
    imof )
        [ $# -ne 1 ] && { service_usage; exit; }
        create_imof
        ;;        

    * )
        service_usage
        ;;
esac
exit 0